﻿using System;
using DddCms.Domain.Utility;
using EntLib = Microsoft.Practices.EnterpriseLibrary.Validation;

namespace DddCms.Infrastructure.Validation
{
    /// <summary>
    /// Exception for validation.
    /// </summary>
    public class ValidationValidException : Exception
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ValidationException"/> class.
        /// </summary>
        /// <param name="message">The message.</param>
        public ValidationValidException(string message) : base(message) { }
    }
    /// <summary>
    /// Exception for validation.
    /// </summary>
    public class ValidationNullException : Exception
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ValidationException"/> class.
        /// </summary>
        /// <param name="message">The message.</param>
        public ValidationNullException(string message) : base(message) { }
    }
    /// <summary>
    /// Valide your values with a list of rules.
    /// </summary>
    public class Validate : IValidate
    {
        /// <summary>
        /// Valids the specified value.
        /// </summary>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="value">The value.</param>
        /// <param name="ruleSetName">Name of the rule set.</param>
        public void Valid<TValue>(TValue value, string ruleSetName)
        {
            // Validate the null params
            Null(value);
            Null(ruleSetName);

            // Call entlib validation
            if (!EntLib::Validation.Validate<TValue>(value, ruleSetName).IsValid)
                throw new ValidationValidException(string.Format("The value of type :{0} is not valid, rule set :{1} is not passed", value.GetType(), ruleSetName));
        }

        /// <summary>
        /// Valide if the specified value is null.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <exception cref="ArgumentNullException">if the value is null</exception>
        public void Null(object value)
        {
            if (value == null)
                throw new ValidationNullException(string.Format("The value is null."));
        }
    }
}
